package com.leef.oauth;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.leef.oauth.common.AppConfigs;
import com.leef.oauth.data.api.RestApi;
import com.leef.oauth.data.pojo.Key;
import com.leef.oauth.data.pojo.Token;
import com.leef.oauth.util.SharePrefsUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;
import timber.log.Timber;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.passwd)
    void oauth() {
        //  构建接口参数
//        Map<String, String> map = new HashMap<>();
//        map.put("client_id", "dd901a9812924c45b8bd28f3f1a3b0bb");
//        map.put("client_secret", "DeAKvWGCcZ554eD5pbLrpk9lH9BP8P2q");
//        map.put("grant_type", "password");
//        map.put("scope", "read");
//        map.put("username", "mobile");
//        map.put("password", "mobile");
        Map<String, String> map = new HashMap<>();
        map.put("client_id", "mobile-client");
        map.put("client_secret", "mobile");
        map.put("grant_type", "password");
        map.put("scope", "read");
        map.put("username", "mobile");
        map.put("password", "96e79218965eb72c92a549dd5a330112");

        Flowable<Response<Token>> request = RestApi.getInstance()
                .getOauthService().oauthPassword(map);

        request.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    Token token = response.body();
                    if (token != null) {
                        SharePrefsUtils.setValue(AppConfigs.ACCESS_TOKEN,
                                token.getAccessToken());
                        SharePrefsUtils.setValue(AppConfigs.REFRESH_TOKEN,
                                token.getRefreshToken());
                        Toast.makeText(this, "Access Token = " + token.getAccessToken(),
                                Toast.LENGTH_SHORT).show();
                    }
                }, Throwable::printStackTrace);
    }

    @OnClick(R.id.resource)
    void request() {
        //  1.从本地获取 access_token
        String access_token = SharePrefsUtils.getString(AppConfigs.ACCESS_TOKEN, "");
        //  2.构建访问资源的接口参数
//        Map<String, String> map = new HashMap<>();
//        map.put("access_token", access_token);
        Map<String, String> map = new HashMap<>();
        map.put("name", "mobile");
        //  3.Create Flow
        Flowable<Response<Key>> request = RestApi.getInstance()
                .getApiService().getKey(map);
        //  4.Request
        request.compose(RestApi.checkTokenIsExpired())
                .onErrorResumeNext(RestApi.refreshToken(request))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    Key key = response.body();
                    if (key != null) {
                        Toast.makeText(this, key.getKey(),
                                Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    Timber.d(throwable, throwable.getMessage());
                });
    }
}
